package edu.java.homework.hw08.ipj10.exer01done;

public class Firm implements TextAgend {
    // --- Instance Variables --------------------------------------------------
    private String name = null;
    private double profit = 0.0d;
    Worker[] workers = null;

    // --- Constructors --------------------------------------------------------

    public Firm(String name, double profit, Worker[] workers) {
	setName(name);
	setProfit(profit);
	setWorkers(workers);
    }

    // --- Getters and Setters -------------------------------------------------

    public void setName(String name) {
	// check formal parametar
	if (null == name) {
	    throw new IllegalArgumentException("Null-pointed string for name");
	}
	this.name = name;
    }

    public void setProfit(double profit) {
	// check formal parametars
	if (profit < 0) {
	    throw new IllegalArgumentException("Profit could not be negative.");
	}
	this.profit = profit;
    }

    public void setWorkers(Worker[] workers) {
	// check for null-point Exception
	if (workers == null) {
	    throw new IllegalArgumentException(
		    "Array of worker objects is null pointed.");
	}
	// check each worker object
	for (Worker worker : workers) {
	    if (worker == null) {
		throw new IllegalArgumentException(
			"There is a null-pointed worker object.");
	    }
	}

	this.workers = workers;
    }

    @Override
    public String getName() {
	return name;
    }

    @Override
    public double getProfit() {
	return profit;
    }

    @Override
    public Worker[] getWorkers() {
	return workers;
    }

    // --- Methods -------------------------------------------------------------

    @Override
    public String toString() {
	StringBuilder sb = new StringBuilder();
	sb.append("Firm name: " + getName() + "\t");
	sb.append("Profit: " + getProfit() + "\n");
	sb.append(printWorkersInfo());

	return sb.toString();
    }

    private String printWorkersInfo() {
	StringBuilder sb = new StringBuilder();
	if (getWorkers() == null) {
	    return null;
	}
	Worker[] workStuff = getWorkers();
	for (int i = 0; i < workStuff.length; i++) {
	    sb.append(workStuff[i].toString() + "\n");
	}
	return sb.toString();
    }
}
